package com.glnz.spring6.resource;

import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

/**
 * @Project ：learn_spring
 * @File ：ClassPathResourceDemo.java
 * @IDE ：IntelliJ IDEA
 * @Author ：自由如风间
 * @Date ：2025/4/5 23:35
 * @Description ：资源操作之class
 */

public class ClassPathResourceDemo {

    public static void main(String[] args) {
        String path = "G:\\codes\\learn_spring\\spring-resources\\src\\main\\resources\\nothing.txt";
        loadClassPathResource(path);
    }

    public static void loadClassPathResource(String path) {
        ClassPathResource resource = new ClassPathResource(path);

        System.out.println(resource.getFilename());
        System.out.println(resource.getDescription());

        // 获取文件内容
        try {
            List<String> lines = Files.readAllLines(Paths.get(path),
                    StandardCharsets.UTF_8);
            lines.forEach(System.out::println);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
